﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.SqlClient;
using System.Data;
using IFRS.DataAccess.DAL;
using IFRS.Models;


namespace IFRS.DataAccess.DAL
{
    public class SPR_SORPORMORCREDIT
    {
        public DataTable SEARCH_ALL(SorPorMorCredit obSPM, Paging paging,bool isAllBranch ,string sCostCenter, ref int iRecordCount)
        {
            DataTable dt = new DataTable();
            IFRS_Utility Utility = new IFRS_Utility();
            List<SqlParameter> lstParams = new List<SqlParameter>();
            try
            {
                lstParams.Add(new SqlParameter("@RecordCount", SqlDbType.Int));
                lstParams[0].Direction = ParameterDirection.Output;
                lstParams.Add(new SqlParameter("@PageIndex", paging.PageIndex));
                lstParams.Add(new SqlParameter("@PageSize", paging.PageSize));
                lstParams.Add(new SqlParameter("@Sorting", paging.Sorting));
                lstParams.Add(new SqlParameter("@isAllBranch", isAllBranch));
                lstParams.Add(new SqlParameter("@sCostCenter", sCostCenter));
                lstParams.Add(new SqlParameter("@TransactionDate", obSPM.TransactionDate.AsDBValue()));
                lstParams.Add(new SqlParameter("@AccountNo", obSPM.AccountNo.AsDBValue()));
                lstParams.Add(new SqlParameter("@CostCenter", obSPM.CostCenter.AsDBValue()));
                lstParams.Add(new SqlParameter("@DebtAdjustFlag", obSPM.DebtAdjustFlag.AsDBValue()));
                lstParams.Add(new SqlParameter("@IsConfirmed", obSPM.IsConfirmed.AsDBValue()));
                dt = Utility.StoredProcDataTable("SPR_SORPORMORCREDIT_SEARCHALL", lstParams.ToArray());
                iRecordCount = (int)lstParams[0].Value;

            }
            catch (Exception ex)
            {
                throw ex;
            }

            return dt;
        }

        public DataTable SEARCH_BYID(int SorPorMorCreditID)
        {
            DataTable dt = new DataTable();
            IFRS_Utility Utility = new IFRS_Utility();
            List<SqlParameter> lstParams = new List<SqlParameter>();
            try
            {
                lstParams.Add(new SqlParameter("@SorPorMorCreditID", SorPorMorCreditID.AsDBValue()));
                dt = Utility.StoredProcDataTable("SPR_SORPORMORCREDIT_SEARCHBYID", lstParams.ToArray());

            }
            catch (Exception ex)
            {
                throw ex;
            }

            return dt;
        }

        public int INSERT(SorPorMorCredit obSPM)
        {

            int iNum = 0;
            IFRS_Utility Utility = new IFRS_Utility();
            List<SqlParameter> lstParams = new List<SqlParameter>();

            try
            {
                lstParams.Add(new SqlParameter("@SorPorMorCreditID", SqlDbType.Int));
                lstParams[0].Direction = ParameterDirection.Output;
                lstParams.Add(new SqlParameter("@TransactionDate", obSPM.TransactionDate.AsDBValue()));
                lstParams.Add(new SqlParameter("@AccountNo", obSPM.AccountNo.AsDBValue()));
                lstParams.Add(new SqlParameter("@GLAccount", obSPM.GLAccount.AsDBValue()));
                lstParams.Add(new SqlParameter("@CostCenter", obSPM.CostCenter.AsDBValue()));
                lstParams.Add(new SqlParameter("@ContractStartDate", obSPM.ContractStartDate.AsDBValue()));
                lstParams.Add(new SqlParameter("@ContractEndDate", obSPM.ContractEndDate.AsDBValue()));
                lstParams.Add(new SqlParameter("@FirstPaymentDate", obSPM.FirstPaymentDate.AsDBValue()));
                lstParams.Add(new SqlParameter("@LastPaymentDate", obSPM.LastPaymentDate.AsDBValue()));
                lstParams.Add(new SqlParameter("@PaymentFreq", obSPM.PaymentFreq.AsDBValue()));
                lstParams.Add(new SqlParameter("@PaymentFreqTimeBase", obSPM.PaymentFreqTimeBase.AsDBValue()));
                lstParams.Add(new SqlParameter("@InterestPayFreq", obSPM.InterestPayFreq.AsDBValue()));
                lstParams.Add(new SqlParameter("@InterestPayFreqTimeBase", obSPM.InterestPayFreqTimeBase.AsDBValue()));
                lstParams.Add(new SqlParameter("@InterestAdjFreq", obSPM.InterestAdjFreq.AsDBValue()));
                lstParams.Add(new SqlParameter("@InterestAdjFreqTimeBase", obSPM.InterestAdjFreqTimeBase.AsDBValue()));
                lstParams.Add(new SqlParameter("@PaymentConditionID", obSPM.PaymentConditionID.AsDBValue()));
                lstParams.Add(new SqlParameter("@PaymentDateID", obSPM.PaymentDateID.AsDBValue()));
                lstParams.Add(new SqlParameter("@CustomerPayFreq", obSPM.CustomerPayFreq.AsDBValue()));
                lstParams.Add(new SqlParameter("@CustomerPayFreqTimeBase", obSPM.CustomerPayFreqTimeBase.AsDBValue()));
                lstParams.Add(new SqlParameter("@TermAmount", obSPM.TermAmount.AsDBValue()));
                lstParams.Add(new SqlParameter("@CapitalAmount", obSPM.CapitalAmount.AsDBValue()));
                lstParams.Add(new SqlParameter("@NextPaymentDate", obSPM.NextPaymentDate.AsDBValue()));
                lstParams.Add(new SqlParameter("@EndCalInterestDate", obSPM.EndCalInterestDate.AsDBValue()));
                lstParams.Add(new SqlParameter("@StartPaymentDate", obSPM.StartPaymentDate.AsDBValue()));
                lstParams.Add(new SqlParameter("@MisPaymentDate", obSPM.MisPaymentDate.AsDBValue()));
                lstParams.Add(new SqlParameter("@NoOfPayment", obSPM.NoOfPayment.AsDBValue()));
                lstParams.Add(new SqlParameter("@UnpaidAmount", obSPM.UnpaidAmount.AsDBValue()));
                lstParams.Add(new SqlParameter("@DayCountMethodID", obSPM.DayCountMethodID.AsDBValue()));
                lstParams.Add(new SqlParameter("@StartInterestRate", obSPM.StartInterestRate.AsDBValue()));
                lstParams.Add(new SqlParameter("@InterestTypeID", obSPM.InterestTypeID.AsDBValue()));
                lstParams.Add(new SqlParameter("@RefInterestCode", obSPM.ReferenceInterestCD.AsDBValue()));
                lstParams.Add(new SqlParameter("@AdditionalInterest", obSPM.AdditionalInterest.AsDBValue()));
                lstParams.Add(new SqlParameter("@InterestRate", obSPM.InterestRate.AsDBValue()));
                lstParams.Add(new SqlParameter("@CurrencyCode", obSPM.CurrencyCode.AsDBValue()));
                lstParams.Add(new SqlParameter("@UnpaidInterest", obSPM.UnpaidInterest.AsDBValue()));
                lstParams.Add(new SqlParameter("@CustomerID", obSPM.CustomerID.AsDBValue()));
                lstParams.Add(new SqlParameter("@CreditLevelID", obSPM.CreditLevelID.AsDBValue()));
                lstParams.Add(new SqlParameter("@ContractLoanTypeID", obSPM.ContractLoanTypeID.AsDBValue()));
                lstParams.Add(new SqlParameter("@LoanObjectiveID", obSPM.LoanObjectiveID.AsDBValue()));
                lstParams.Add(new SqlParameter("@CollateralValue", obSPM.CollateralValue.AsDBValue()));
                lstParams.Add(new SqlParameter("@ReservedAmount", obSPM.ReservedAmount.AsDBValue()));
                lstParams.Add(new SqlParameter("@DebtAdjustFlag", obSPM.DebtAdjustFlag.AsDBValue()));
                lstParams.Add(new SqlParameter("@IsConfirmed", obSPM.IsConfirmed.AsDBValue()));
                lstParams.Add(new SqlParameter("@RP_GSB_PRDCD", obSPM.RP_GSB_PRDCD.AsDBValue()));
                lstParams.Add(new SqlParameter("@RP_SOURCE_FILE", obSPM.RP_SOURCE_FILE.AsDBValue()));
                lstParams.Add(new SqlParameter("@RP_MAPPING_CD", obSPM.RP_MAPPING_CD.AsDBValue()));
                lstParams.Add(new SqlParameter("@CreatedDate", obSPM.CreatedDate.AsDBValue()));
                lstParams.Add(new SqlParameter("@CreatedBy", obSPM.CreatedBy.AsDBValue()));
                lstParams.Add(new SqlParameter("@CreatedCostCenter", obSPM.CreatedCostCenter.AsDBValue()));
                lstParams.Add(new SqlParameter("@CreatedBranch", obSPM.CreatedBranch.AsDBValue()));
                lstParams.Add(new SqlParameter("@UpdatedDate", obSPM.UpdatedDate.AsDBValue()));
                lstParams.Add(new SqlParameter("@UpdatedBy", obSPM.UpdatedBy.AsDBValue()));
                lstParams.Add(new SqlParameter("@UpdatedCostCenter", obSPM.UpdatedCostCenter.AsDBValue()));
                lstParams.Add(new SqlParameter("@UpdatedBranch", obSPM.UpdatedBranch.AsDBValue()));

                Utility.ExecuteStoredProc("SPR_SORPORMORCREDIT_INSERT", lstParams.ToArray());

                iNum = (int)lstParams[0].Value;

            }
            catch (Exception ex)
            {
                throw ex;
            }

            return iNum;
        }

        public int UPDATE(SorPorMorCredit obSPM)
        {


            int iNum = 0;
            IFRS_Utility Utility = new IFRS_Utility();
            List<SqlParameter> lstParams = new List<SqlParameter>();
            try
            {
                lstParams.Add(new SqlParameter("@SorPorMorCreditID", obSPM.SorPorMorCreditID.AsDBValue()));
                lstParams.Add(new SqlParameter("@TransactionDate", obSPM.TransactionDate.AsDBValue()));
                lstParams.Add(new SqlParameter("@AccountNo", obSPM.AccountNo.AsDBValue()));
                lstParams.Add(new SqlParameter("@GLAccount", obSPM.GLAccount.AsDBValue()));
                lstParams.Add(new SqlParameter("@CostCenter", obSPM.CostCenter.AsDBValue()));
                lstParams.Add(new SqlParameter("@ContractStartDate", obSPM.ContractStartDate.AsDBValue()));
                lstParams.Add(new SqlParameter("@ContractEndDate", obSPM.ContractEndDate.AsDBValue()));
                lstParams.Add(new SqlParameter("@FirstPaymentDate", obSPM.FirstPaymentDate.AsDBValue()));
                lstParams.Add(new SqlParameter("@LastPaymentDate", obSPM.LastPaymentDate.AsDBValue()));
                lstParams.Add(new SqlParameter("@PaymentFreq", obSPM.PaymentFreq.AsDBValue()));
                lstParams.Add(new SqlParameter("@PaymentFreqTimeBase", obSPM.PaymentFreqTimeBase.AsDBValue()));
                lstParams.Add(new SqlParameter("@InterestPayFreq", obSPM.InterestPayFreq.AsDBValue()));
                lstParams.Add(new SqlParameter("@InterestPayFreqTimeBase", obSPM.InterestPayFreqTimeBase.AsDBValue()));
                lstParams.Add(new SqlParameter("@InterestAdjFreq", obSPM.InterestAdjFreq.AsDBValue()));
                lstParams.Add(new SqlParameter("@InterestAdjFreqTimeBase", obSPM.InterestAdjFreqTimeBase.AsDBValue()));
                lstParams.Add(new SqlParameter("@PaymentConditionID", obSPM.PaymentConditionID.AsDBValue()));
                lstParams.Add(new SqlParameter("@PaymentDateID", obSPM.PaymentDateID.AsDBValue()));
                lstParams.Add(new SqlParameter("@CustomerPayFreq", obSPM.CustomerPayFreq.AsDBValue()));
                lstParams.Add(new SqlParameter("@CustomerPayFreqTimeBase", obSPM.CustomerPayFreqTimeBase.AsDBValue()));
                lstParams.Add(new SqlParameter("@TermAmount", obSPM.TermAmount.AsDBValue()));
                lstParams.Add(new SqlParameter("@CapitalAmount", obSPM.CapitalAmount.AsDBValue()));
                lstParams.Add(new SqlParameter("@NextPaymentDate", obSPM.NextPaymentDate.AsDBValue()));
                lstParams.Add(new SqlParameter("@EndCalInterestDate", obSPM.EndCalInterestDate.AsDBValue()));
                lstParams.Add(new SqlParameter("@StartPaymentDate", obSPM.StartPaymentDate.AsDBValue()));
                lstParams.Add(new SqlParameter("@MisPaymentDate", obSPM.MisPaymentDate.AsDBValue()));
                lstParams.Add(new SqlParameter("@NoOfPayment", obSPM.NoOfPayment.AsDBValue()));
                lstParams.Add(new SqlParameter("@UnpaidAmount", obSPM.UnpaidAmount.AsDBValue()));
                lstParams.Add(new SqlParameter("@DayCountMethodID", obSPM.DayCountMethodID.AsDBValue()));
                lstParams.Add(new SqlParameter("@StartInterestRate", obSPM.StartInterestRate.AsDBValue()));
                lstParams.Add(new SqlParameter("@InterestTypeID", obSPM.InterestTypeID.AsDBValue()));
                lstParams.Add(new SqlParameter("@RefInterestCode", obSPM.ReferenceInterestCD.AsDBValue()));
                lstParams.Add(new SqlParameter("@AdditionalInterest", obSPM.AdditionalInterest.AsDBValue()));
                lstParams.Add(new SqlParameter("@InterestRate", obSPM.InterestRate.AsDBValue()));
                lstParams.Add(new SqlParameter("@CurrencyCode", obSPM.CurrencyCode.AsDBValue()));
                lstParams.Add(new SqlParameter("@UnpaidInterest", obSPM.UnpaidInterest.AsDBValue()));
                lstParams.Add(new SqlParameter("@CustomerID", obSPM.CustomerID.AsDBValue()));
                lstParams.Add(new SqlParameter("@CreditLevelID", obSPM.CreditLevelID.AsDBValue()));
                lstParams.Add(new SqlParameter("@ContractLoanTypeID", obSPM.ContractLoanTypeID.AsDBValue()));
                lstParams.Add(new SqlParameter("@LoanObjectiveID", obSPM.LoanObjectiveID.AsDBValue()));
                lstParams.Add(new SqlParameter("@CollateralValue", obSPM.CollateralValue.AsDBValue()));
                lstParams.Add(new SqlParameter("@ReservedAmount", obSPM.ReservedAmount.AsDBValue()));
                lstParams.Add(new SqlParameter("@DebtAdjustFlag", obSPM.DebtAdjustFlag.AsDBValue()));
                lstParams.Add(new SqlParameter("@IsConfirmed", obSPM.IsConfirmed.AsDBValue()));
                lstParams.Add(new SqlParameter("@RP_GSB_PRDCD", obSPM.RP_GSB_PRDCD.AsDBValue()));
                lstParams.Add(new SqlParameter("@RP_SOURCE_FILE", obSPM.RP_SOURCE_FILE.AsDBValue()));
                lstParams.Add(new SqlParameter("@RP_MAPPING_CD", obSPM.RP_MAPPING_CD.AsDBValue()));
                lstParams.Add(new SqlParameter("@CreatedDate", obSPM.CreatedDate.AsDBValue()));
                lstParams.Add(new SqlParameter("@CreatedBy", obSPM.CreatedBy.AsDBValue()));
                lstParams.Add(new SqlParameter("@CreatedCostCenter", obSPM.CreatedCostCenter.AsDBValue()));
                lstParams.Add(new SqlParameter("@CreatedBranch", obSPM.CreatedBranch.AsDBValue()));
                lstParams.Add(new SqlParameter("@UpdatedDate", obSPM.UpdatedDate.AsDBValue()));
                lstParams.Add(new SqlParameter("@UpdatedBy", obSPM.UpdatedBy.AsDBValue()));
                lstParams.Add(new SqlParameter("@UpdatedCostCenter", obSPM.UpdatedCostCenter.AsDBValue()));
                lstParams.Add(new SqlParameter("@UpdatedBranch", obSPM.UpdatedBranch.AsDBValue()));
                iNum = (int)Utility.ExecuteStoredProc("SPR_SORPORMORCREDIT_UPDATE", lstParams.ToArray());

      
	


            }
            catch (Exception ex)
            {
                throw ex;
            }

            return iNum;
        }

        public int DELETE(SorPorMorCredit obSPM)
        {
            int iNum = 0;
            IFRS_Utility Utility = new IFRS_Utility();
            List<SqlParameter> lstParams = new List<SqlParameter>();
            try
            {
                lstParams.Add(new SqlParameter("@SorPorMorCreditID", obSPM.SorPorMorCreditID.AsDBValue()));
                lstParams.Add(new SqlParameter("@UpdatedDate", obSPM.UpdatedDate.AsDBValue()));
                lstParams.Add(new SqlParameter("@UpdatedBy", obSPM.UpdatedBy.AsDBValue()));
                lstParams.Add(new SqlParameter("@UpdatedCostCenter", obSPM.UpdatedCostCenter.AsDBValue()));
                lstParams.Add(new SqlParameter("@UpdatedBranch", obSPM.UpdatedBranch.AsDBValue()));
                iNum = (int)Utility.ExecuteStoredProc("SPR_SORPORMORCREDIT_DELETE", lstParams.ToArray());

            }
            catch (Exception ex)
            {
                throw ex;
            }

            return iNum;
        }

    }
}
